package com.example.app_mqttclient.obj;

import android.util.Log;

public class DataAnalyse {

    private static final String TAG = "DataAnalyse";

    /**
     * @brief
     * @param  source: 数据源
     * @param  split: 分隔符
     * @param  key: 键
     * @param  value: 值
     * @retval false: 无目标数据  true: 有目标数据
     */
    public static boolean stringFindKeyword(String source, String split, String key, String value)
    {
        if((source.indexOf(split) == -1) || (source.isEmpty()))
        {
            return false;
        }
        String[] stringArray = source.split(split);
        String stringTemp = null;
        for(int i = 0; i < stringArray.length; i++)
        {
            if(stringArray[i].indexOf(key) != -1)
            {
                stringTemp = stringArray[i];
                Log.i(TAG, "string find key-value " + stringTemp);
                break;
            }
        }
        if(stringTemp != null)
        {
            stringArray = stringTemp.split(":");
            stringTemp = stringArray[1];
            Log.i(TAG, "string find value " + stringTemp);
            if(stringTemp.equals(value))
            {
                return true;
            }
        }
        else
        {
            return false;
        }
        return false;
    }

    /**
     * @brief
     * @param  source: 数据源
     * @param  split: 分隔符
     * @param  key: 键
     * @retval 返回目标键的值
     */
    public static String stringReturnValue(String source, String split, String key)
    {
        if((source.indexOf(split) == -1) || (source.isEmpty()))
        {
            return null;
        }
        String[] stringArray = source.split(split);
        String stringTemp = null;
        for(int i = 0; i < stringArray.length; i++)
        {
            if(stringArray[i].indexOf(key) != -1)
            {
                stringTemp = stringArray[i];
                Log.i(TAG, "string find key-value " + stringTemp);
                break;
            }
        }
        if(stringTemp != null)
        {
            stringArray = stringTemp.split(":");
            stringTemp = stringArray[1];
            Log.i(TAG, "string find value " + stringTemp);
        }
        return stringTemp;
    }

    /**
     * @brief
     * @param  source: 数据源
     * @param  split: 分隔符
     * @retval 返回首个键
     */
    public static String stringFirstKey(String source, String split)
    {
        if((source.indexOf(split) == -1) || (source.isEmpty()))
        {
            return null;
        }
        String[] stringArray = source.split(split);
        return stringArray[0];
    }
}
